/*------------------------------------------------------------------------------------------------\
| Horrible X-Wing game by Steven Haber
| st_haber@hotmail.com
|
| health.cpp: contains class Health, which holds the health of a player and displays a health bar
\------------------------------------------------------------------------------------------------*/

#include "Health.h"


//Constructor takes two sprites, a render pointer, and the position of the bar
Health::Health(sf::Sprite *sprites[], sf::RenderWindow *out, int posX, int posY, Sound *sounds)
{
	mSpriteUnder = *sprites[HEALTH_U];
	mSpriteOver = *sprites[HEALTH_O];
	mHealth = 100;
	mOut = out;
	mSounds = sounds;

	mSpriteUnder.SetTop((float)posY);
	mSpriteOver.SetTop((float)posY);
	mSpriteUnder.SetLeft((float)posX);
	mSpriteOver.SetLeft((float)posX);
}


//Hurts, if health <= 0 then player is dead (false)
bool Health::hurt(int amount)
{
	mSounds->playSound(HURT_PLAYER);

	mHealth -= amount;
	
	if(mHealth <= 0)
	{
		mHealth = 1;
		return false;
	}
	return true;
}


//Draws to buffer, scales overlay based on current health
void Health::drawHealth()
{
	mOut -> Draw(mSpriteUnder);
	mSpriteOver.SetScaleX((float)mHealth/100);
	mOut -> Draw(mSpriteOver);
}